package medium

func removeKdigits(num string, k int) string {
	var stack []byte
	for i := range num {
		digit := num[i]
		for k > 0 && len(stack) > 0 && digit < stack[len(stack)-1] {
			stack = stack[:len(stack)-1]
			k--
		}
		stack = append(stack, digit)
	}
	stack = stack[:len(stack)-k]
	str := string(stack)
	if len(str) == 0 {
		return "0"
	}

	for len(str) > 1 && str[0] == '0' {
		str = str[1:]
	}
	return str

}
